package com.siriframe.security.service.impl;

import com.siriframe.entity.Result;
import com.siriframe.security.dao.MenuMapper;
import com.siriframe.security.dao.UserMapper;
import com.siriframe.security.dao.UserRoleMapper;
import com.siriframe.security.entity.po.User;
import com.siriframe.security.entity.po.UserRole;
import com.siriframe.security.enums.Role;
import com.siriframe.security.service.UserManagementService;
import com.siriframe.security.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserManagementServiceImpl implements UserManagementService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Result updatePermission(Long userId, String role) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        if(Role.SYSTEM_ADMINISTRATOR.getRoleName().equals(role)){
            userRole.setRoleId(Role.SYSTEM_ADMINISTRATOR.getRoleId());
        }else if(Role.USER_COMMON.getRoleName().equals(role)){
            userRole.setRoleId(Role.USER_COMMON.getRoleId());
        }else if(Role.USER_PROVIDER.getRoleName().equals(role)){
            userRole.setRoleId(Role.USER_PROVIDER.getRoleId());
        }else{
            return Result.error(500, "该角色不存在");
        }
        int update = userRoleMapper.updateById(userRole);
        if(update == 1){
            return Result.ok("修改成功");
        }
        return Result.error(500, "修改时出现异常，请重试");
    }

    @Override
    public Result getUserInformationByToken(HttpServletRequest request) {
        //获取token
        String token = request.getHeader("token");
        if(!StringUtils.hasText(token)){
            //放行
            throw new RuntimeException("token非法或token失效");
        }
        //解析token
        String id;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            id = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }

        Long userInformationId = Long.parseLong(id);

        User user = userMapper.selectById(userInformationId);

        List<String> list = menuMapper.selectPermsByUserId(user.getId());


        Map map = new HashMap();
        map.put("User信息", user);
        map.put("User的角色", list);

        return Result.ok("返回角色信息数据",map);
    }

    @Override
    public Result getUserInformationByUserId(Long userId) {
        User user = userMapper.selectById(userId);
        List<String> list = menuMapper.selectPermsByUserId(userId);

        Map map = new HashMap();
        map.put("User信息", user);
        map.put("User的角色", list);

        return Result.ok("返回角色信息数据",map);
    }
}
